package model

import (
	"database/sql"
	"errors"
	"fmt"
	g "github.com/doug-martin/goqu/v9"
	"github.com/valyala/fasthttp"
	"strconv"
)

type TblFakeRtp struct {
	Id         int     `json:"id" db:"id" cbor:"id"`
	Game       string  `json:"game" db:"game" cbor:"game"`
	OperatorId string  `db:"operator_id" json:"operator_id"`
	Money      float64 `db:"money" json:"money"`
	RTP        float64 `db:"rtp" json:"rtp"`
}

type TblFakeRtpParam struct {
	Page     int `json:"page"`
	PageSize int `json:"page_size"`

	Id         int     `json:"id" db:"id" cbor:"id"`
	Game       string  `json:"game" db:"game" cbor:"game"`
	OperatorId string  `db:"operator_id" json:"operator_id"`
	Money      float64 `db:"money" json:"money"`
	RTP        float64 `db:"rtp" json:"rtp"`
}

type TblFakeRtpList struct {
	D []TblFakeRtp `json:"d" cbor:"d"`
	T int          `json:"total" cbor:"t"`
}

func GetTblFakeRtp(param TblFakeRtpParam, ctx *fasthttp.RequestCtx) (TblFakeRtp, error) {
	data := TblFakeRtp{}
	table := "tbl_cfg_fake_rtp"
	where := "1=1"
	join := ""
	loginUser := GetLoginUser(ctx)
	if loginUser.Businsess != "" {
		where += " and 1=2"
	}
	if param.OperatorId != "" {
		where += " and a.operator_id=" + param.OperatorId
	}
	if loginUser.Operator != "" {
		where += " and a.operator_id=" + loginUser.Operator
	}
	if param.Game != "" {
		where += " and a.game='" + param.Game + "'"
	}
	field := "a.*"
	query := "select " + field + " from " + table + "  as a " + join + " where " + where
	err := meta.MerchantDB.Get(&data, query)
	if err != nil && err != sql.ErrNoRows {
		return data, pushLog(fmt.Errorf("%s,[%s]", err.Error(), query), "数据库错误")
	}
	return data, nil
}

func GetTblFakeRtpList(param TblFakeRtpParam, ctx *fasthttp.RequestCtx) (TblFakeRtpList, error) {
	data := TblFakeRtpList{}
	table := "tbl_cfg_fake_rtp"
	where := "1=1"
	join := ""
	oderBy := "a.id"
	oderType := "desc"
	loginUser := GetLoginUser(ctx)
	if loginUser.Businsess != "" {
		where += " and 1=2"
	}
	if param.OperatorId != "" {
		where += " and operator_id=" + param.OperatorId
	}
	if loginUser.Operator != "" {
		where += " and operator_id=" + loginUser.Operator
	}
	if param.Game != "" {
		where += " and a.game='" + param.Game + "'"
	}
	order := oderBy + " " + oderType

	offset := (param.Page - 1) * param.PageSize
	if param.Page >= 1 {
		count := "select count(1) from " + table + " as a where " + where
		err := meta.MerchantDB.Get(&data.T, count)
		if err != nil {
			return data, pushLog(fmt.Errorf("%s,[%s]", err.Error(), count), "数据库错误")
		}
		if data.T == 0 {
			return data, nil
		}
	}

	field := "a.*"
	query := "select " + field + " from " + table + "  as a " + join + " where " + where + " order by " + order + " limit " + strconv.Itoa(param.PageSize) + " offset " + strconv.Itoa(offset)
	err := meta.MerchantDB.Select(&data.D, query)
	if err != nil && err != sql.ErrNoRows {
		return data, pushLog(fmt.Errorf("%s,[%s]", err.Error(), query), "数据库错误")
	}
	return data, nil
}

func UpdateOrDelTblFakeRtp(param TblFakeRtp, ctx *fasthttp.RequestCtx, isDelete int) error {
	loginUser := GetLoginUser(ctx)
	if loginUser.Operator != "" {
		param.OperatorId = loginUser.Operator
	}

	table := "tbl_cfg_fake_rtp"
	query := ""
	record := g.Record{
		"game":        param.Game,
		"money":       param.Money,
		"rtp":         param.RTP,
		"operator_id": param.OperatorId,
	}
	if param.Id > 0 {
		data := TblFakeRtp{}
		t := dialect.From(table)
		query, _, _ = t.Select("*").Where(g.Ex{"id": param.Id}).Limit(1).ToSQL()

		_ = meta.MerchantDB.Get(&data, query)
		if loginUser.Operator != "" {
			if data.OperatorId != loginUser.Operator {
				return errors.New("权限不足")
			}
		}

		if isDelete == 0 {
			query, _, _ = dialect.Update(table).Set(record).Where(g.Ex{"id": param.Id}).ToSQL()
		} else {
			query, _, _ = dialect.Delete(table).Where(g.Ex{"id": param.Id}).ToSQL()
		}
	} else {
		query, _, _ = dialect.Insert(table).Rows(record).ToSQL()
	}
	_, err := meta.MerchantDB.Exec(query)
	if err != nil {
		return errors.New("数据库错误")
	}
	return nil
}
